<?php
require_once('./wp-admin/includes/admin.php');
require_once('./wp-admin/includes/user.php');
if (!current_user_can('publish_posts')) {
    echo 'ingen tilgang.';
} else {
    ?>

    <?php
    global $hide_right_sidebar;
    $hide_right_sidebar = true;

    if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == "edit_post" && isset($_POST['postid'])) {
        $post_to_edit = array();
        $post_to_edit = get_post($_POST['postid']);

        /* these are the fields that we are editing in the form below. you have to change them to your fields and you can add as many as you need. */

        $title = $_POST['title'];
        $description = $_POST['description'];

        /* this code will save the title and description into the post_to_edit array */

        $post_to_edit->post_title = $title;
        $post_to_edit->post_content = $description;

        
        /* honestly i can't really remember why i added this code but it is a must */

        $pid = wp_update_post($post_to_edit);

        /* save taxonomies: post ID, form name, taxonomy name, if it appends(true) or rewrite(false) */
        /* here you have to change the "coupon_categories" and "coupon_tags" to the name of your taxonomies */

        wp_set_post_terms($pid, array($_POST['cat']), 'categories', false);
        wp_set_post_terms($pid, array($_POST['post_tags']), 'tags', false);

//UPDATE CUSTOM FIELDS WITH THE NEW INFO
//CHANGE TO YOUR CUSTOM FIELDS AND ADD AS MANY AS YOU NEED
//        update_post_meta($pid, 'street', $street); // CHANGE "STREET" TO YOUR FIELD NAME AND CHANGE "$STREET" ONLY IF YOU CHANGED IT ALSO IN THE CODE ABOVE
//        update_post_meta($pid, 'apt', $apt);       // CHANGE "APT" TO YOUR FIELD NAME AND CHANGE "$APT" ONLY IF YOU CHANGED IT ALSO IN THE CODE ABOVE
//REDIRECT USER WHERE EVER YOU WANT AFTER DONE EDITING
        wp_redirect($_POST['fra-side']);
exit;
//INSERT OUR MEDIA ATTACHMENTS
// THE FIRST LINE OF THE CODE AS TO DO WITH A LITTLE JAVASCRIPT THAT I WILL EXPLAIN LATER. IT CHECKS IF WE NEED TO CHANGE THE IMAGE or not
//        if ($_FILES) {
//            if ($_POST['change_image'] == 1) {
//                foreach ($_FILES as $file => $array) {
//
//                    $newupload = insert_attachment($file, $pid);
//                    // $newupload returns the attachment id of the file that
//                    // was just uploaded. Do whatever you want with that now.
//                }
//            } // END THE IF STATEMENT FOR FILES
//        } // END THE IF STATEMENT FOR if to upload image
    } // end check for errors
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
?>

<?php get_header(); ?>
<!--<script language="vbscript" type="text/vbs">

    function showMe (it, box) {
    var vis = (box.checked) ? "visible" : "hidden";
    $img = (box.checked) ? document.edit_post.change_image.value = "1" : document.edit_post.change_image.value = "0" ; // send the right value if to change image or not
    document.getElementById(it).style.visibility = vis;
    ;
    }
</script>-->

<body onload="showMe('image', 'c1')">

    <?php $post_to_edit = get_post($_POST['nyhetID']); ?>
    <?php $terms = get_the_terms($post_to_edit->ID, 'category'); ?>
    <?php $tags = strip_tags(get_the_term_list($post_to_edit->ID, 'post_tag', '', ', ', '')); ?>

    <?php $term_name = strip_tags(get_the_term_list($post_to_edit->ID, 'category', '', ', ', '')); ?> <!-- get the category name of this post -->
    <?php $term_obj = get_term_by('name', $term_name, 'category'); ?> <!-- get the current term object -->
    <?php $term_id = $term_obj->term_id; ?> <!-- get this post's term id -->
    <?php
    $args = array(
        'selected' => $term_id,
        'name' => 'cat',
        'class' => 'postform',
        'tab_index' => 10,
        'depth' => 2,
        'hierarchical' => 1,
        'taxonomy' => 'category',
        'hide_empty' => false);
    ?> <?php /* array for wp_dropdown_category to display with the current post category selected by default */ ?>

    <div id="content" role="main">

        <div class="new-form">

            <!-- EDIT COUPON FORM -->

            <form id="edit_post" name="edit_post" method="post" action="" enctype="multipart/form-data">

                <!-- post name -->
                <fieldset name="name">
                    <label for="title">Nyhets tittel:</label><br />
                    <input type="text" id="title" value="<?php echo $post_to_edit->post_title; ?>" tabindex="5" name="title" /> <!-- TITLE FIELD. NOTHING TO CHANGE -->
                </fieldset>

                <!-- post Category -->
                <fieldset id="category">
                    <label for="cat" ><?php wp_dropdown_categories($args); ?></label> <!-- DROP DOWN WITH THE $ARG THAT WE CREATED BEFORE -->
                </fieldset>

                <!-- post Content -->
                <fieldset class="content">
                    <label for="description">Innhold:</label><br />
                    <?php $content = $post_to_edit->post_content; ?>
                    <?php wp_editor($content, 'description'); ?> 
                    <!--<textarea id="description"  tabindex="15" name="description"><?php // echo $post_to_edit->post_content;  ?></textarea>  TEXT AREA OF CONTENT. NOTHING TO CHANGE -->
                </fieldset>

                

                <!--<h3> business's full address: </h3>-->

                <!-- BELOW ARE THE CUSTOM FIELDS. CHANGE THEM ADD OR REMOVE -->

                <!--                <fieldset class="street">
                                    <label for="street">Street:</label><br />
                                    <input type="text" value="<?php // echo get_post_meta($post_to_edit->ID, 'street', true);   ?>" id="street" tabindex="20" name="street" />  CHANGE "STREET" TO NAME OF YOUR FIELD 
                                </fieldset>
                
                                <fieldset class="apt">
                                    <label for="apt">Apt/Suit Number:</label><br />
                                    <input type="text" value="<?php // echo get_post_meta($post_to_edit->ID, 'apt', true);   ?>" id="apt" tabindex="20" name="apt" />  CHANGE "APT" TO NAME OF YOUR FIELD 
                                </fieldset>-->

                <!-- post tags -->
                <fieldset class="tags">
                    <label for="post_tags">Søkeord for nyhet (separert med komma):</label><br />
                    <input type="text" value="<?php echo $tags; ?>" tabindex="35" name="post_tags" id="post_tags" /> <!-- POST TAGS. NOTING TO CHANGE -->
                </fieldset>

                <fieldset class="submit">
                    <input type="submit" value="Oppdater" tabindex="40" id="submit" name="submit" /> <!-- SUBMIT BUTTON -->

                </fieldset>
                <input type="hidden" name="postid" value="<?php echo $post_to_edit->ID; ?>" /> <!-- DONT REMOVE OR CHANGE -->
                <input type="hidden" name="action" value="Oppdater" />			<!-- DONT REMOVE OR CHANGE -->
                <input type="hidden" name="change_cat" value="" />		<!-- DONT REMOVE OR CHANGE -->
                <input type="hidden" name="change_image" value="" />		<!-- DONT REMOVE OR CHANGE -->
                <?php // wp_nonce_field( 'new-post' );   ?>
            </form>
            <!-- END OF FORM -->

        </div><!-- .entry-content -->
    </div><!-- #post-## -->

